package com.matt.controller.estimate;


import com.dotflat.util.excelUtil.Constants;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.estimate.CourseExtendEstimate;
import com.matt.domain.estimate.LikedRecord;
import com.matt.domain.estimate.dto.CourseEstimateDTO;
import com.matt.service.estimate.CourseEstimateService;
import com.matt.service.estimate.LikedRecordService;
import com.matt.service.remoteCall.SsoCallService;
import com.tuo51.components.redis.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/estimate/course")
@Slf4j
@Api(tags = "课程评价")
public class CourseEstimateController {
    @Autowired
    private CourseEstimateService courseEstimateService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SsoCallService ssoCallService;

    @GetMapping("/list")
    @ApiOperation(value = "查询课程评论")
    public StatusResponse courseList(@RequestHeader(TICKET) String ticket, String courseId, Integer pageNum, Integer pageSize) {
        Page<CourseEstimateDTO> page = courseEstimateService.getPage(ticket, courseId, pageNum, pageSize);
        return StatusResponse.success(page);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增课程评论")
    public StatusResponse addCourseEstimate(@RequestHeader(TICKET) String ticket, @RequestBody CourseExtendEstimate courseExtendEstimate) {
        CourseExtendEstimate courseExtendEstimate1 = courseEstimateService.addCourseExtendEstimate(ticket, courseExtendEstimate);
        return StatusResponse.success(courseExtendEstimate1);
    }

    @PostMapping("/update")
    @ApiOperation(value = "查询课程评论")
    public StatusResponse updateCourseEstimate(@RequestHeader(TICKET) String ticket, @RequestBody CourseExtendEstimate courseExtendEstimate) {

        return StatusResponse.success();
    }

    @PostMapping("/like")
    @ApiOperation(value = "点赞/取消点赞")
    public StatusResponse like(@RequestHeader(TICKET) String ticket, @RequestBody CourseEstimateDTO courseEstimateDTO) {
        // 点赞时间限制
        StaffDTO staffDTO = ssoCallService.getStaffDTO(ticket);
        Object o = redisTemplate.opsForValue().get(Constants.LIKE_LIMIT + staffDTO.getId());
        if (Objects.nonNull(o)) {
            return StatusResponse.error("4001", "点击过快，请稍后重试！");
        }
        // 存储点赞限制
        redisTemplate.opsForValue().set(Constants.LIKE_LIMIT + staffDTO.getId(), staffDTO.getId(), 1, TimeUnit.SECONDS);

        CourseEstimateDTO courseEstimateLikeDTO = courseEstimateService.like(ticket, courseEstimateDTO);
        return StatusResponse.success(courseEstimateLikeDTO);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "查询课程评论")
    public StatusResponse deleteCourseEstimate(@RequestHeader(TICKET) String ticket, String courseId, String courseEstimateId) {
        CourseExtendEstimate courseExtendEstimate = courseEstimateService.deleteCourseExtendEstimate(ticket, courseId, courseEstimateId);
        return StatusResponse.success(courseExtendEstimate);
    }
}
